<?php
namespace Admin\Controller;
use Think\Controller;

class GoodsController extends CommonController {

	//商品列表页
    public function index(){
        $goods = M('Goods'); // 实例化goods数据对象  date 是你的表名
        $count = $goods->join('goods_class ON goods.good_class_id = goods_class.id')->where('good_state=1')->count();// 查询满足要求的总记录数 'state=0'表示查询条件
        $Page = new \Think\Page($count,10);// 实例化分页类 传入总记录数
        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询
        //关联goods与goods_class两张表,field取出goods_class中的name
        $list = $goods->join('goods_class ON goods.good_class_id = goods_class.id')->field('goods.id as gid,name,good_title,good_price,good_keyword,stock,good_state,shelf_time')->order('gid')->limit($Page->firstRow.','.$Page->listRows)->where('good_state=1')->select(); // $Page->firstRow 起始条数 $Page->listRows 获取多少条
        $this->assign('list',$list);// 赋值数据集
        $this->assign('page',$show);// 赋值分页输出
        $this->display(); // 输出模板
    }

    //获取分类，再跳转到save方法
	public function add(){
        $goods = D("Goods_class")->select();//查询分类表信息
        $id = I('get.id',0);//获取$_GET['id']并赋值给id变量,如果没有获取到则返回0
        if($id>0){
            $update = M('Goods')->find($id);//查找对应id商品信息
            $this->assign("id",$id);//定义一个id变量，值为$id
            $this->assign("update",$update);//定义一个update变量，值为$update
            $ImgModel = M("Goods_img");
            $imgs = $ImgModel->where('good_id='.$id)->select();
            $this->assign("imgs",$imgs);

        }
        $this->action = U('Admin/goods/save');
        $this->assign("classes",$goods);
		$this->display();
	}

    //添加商品或者修改商品
    public function save(){
        if (IS_POST) {
            $id = I('post.id',0);
            //如果用户提交数据
            $goods = D("Goods");
            $ImgModel = M("Goods_img");
            if (!$goods->create()) {
                // 如果创建失败 表示验证没有通过 输出错误提示信息
                $this->error($goods->getError());
                exit();
            } else {
                if (!$id) {
                    $id = $goods->add();
                    if($id>0){
                        $upload = new \Think\Upload;// 实例化上传类
                        $info = $upload->upload();
                        $data =array();
                        foreach ($info as $key => $value) {
                            $url = $value['savepath'].$value['savename'];
                            $data[]=array('name'=>$value['name'],'url'=>$url,'good_id'=>$id,'sort'=>0);
                        }
                        $ImgModel->addAll($data);
                    }
                    $this->success("添加成功",'index');
                } else {
                    $goods->save();
                    $upload = new \Think\Upload;// 实例化上传类
                    $info = $upload->upload();
                    $data =array();
                    foreach ($info as $key => $value) {
                        $url = $value['savepath'].$value['savename'];
                        $data[]=array('name'=>$value['name'],'url'=>$url,'good_id'=>$id);
                    }
                    $ImgModel->addAll($data);
                    $this->success("修改成功",'index');
                }
                
                
            }
        }else{
            $this->error("添加失败");
        }
    }
 
    //删除商品
    public function delete($id){
        $delete = M('goods');
        $delete_img = M('goods_img');
        $delete->delete($id);
        $delete_img->where('good_id='.$id)->delete();
        if($delete&&$delete_img){
            $this->success("删除成功", U('goods/index'));
        }else{
            $this->error("删除失败");
        }
    }
}